<?php
/* 
    Cette classe permet de gérer l'aspect nouveau et ancien d'un élément comme un topic, une fiche, une aventure, etc...
 */
class OldNewStuffManager{
    public $old_stuff;
    private $user;
    public static $_delay = 604800;
    public function __construct(sfGuardUser $user = null){
        if($user != null && $user->isAuthenticated()){
            $this->user = $user;
            $this->loadOldStuff();
            $this->cleanRecords();
        }
    }
    public function setUser($user){
        if($user != NULL){
            if($user->isAuthenticated()){
                $this->user = $user;
                $this->loadOldStuff();
                $this->cleanRecords();
            }
        }
    }
    public function compareTime($created_at){
        $compare_time = strtotime($created_at) + OldNewStuffManager::$_delay;
        return strtotime($this->user->getGuardUser()->getLastLogin()) > $compare_time;
    }
    public function isNewAdventure(KTDAdventure $adv){
        if($this->user == NULL)
            return false;
        if($this->compareTime($adv->getLastAction()->getCreatedAt()))
            return false;
        else{
            if($this->hasOldStuff('aventure', $adv->getLastAction()->getId()))
                return false;
            else
                return true;
        }
    }
    /*Retourne si le joueur a reçu de nouveaux mp. Soit 0 = false, soit le nb de nouveaux mp*/
    public function countNewPrivateMessage(){
        if($this->user != NULL)
            return false;
        return KTDPrivateMessage::countNewPrivateMessage($user->getGuardUser());
    }
    public function isNewTopic(KTDTopic $topic){
        if($this->user == NULL)
            return false;
        if($this->compareTime($topic->getLastMessage()->getCreatedAt()))
            return false;
        else{
            if($this->hasOldStuff('topic', $topic->getLastMessage()->getId()))
                return false;
            else
                return true;
        }
    }
    public function hasNewFicheComment(KTDCharacter $character){
       $lct = $character->getLastComment();
       if($this->user == NULL)
            return false;
       if(!$lct)
           return false;
       if($this->compareTime($lct->getCreatedAt()))
        return false;
       else{
           if($this->hasOldStuff('fiche_comment', $lct->getId()))
                   return false;
           else
               return true;
       }
        
    }
    public function setOldFicheComment(KTDFicheComment $lct){
        if($this->user != NULL){
            $old_lct = $this->createOldStuff("fiche_comment", $lct->getId(), $lct->getCreatedAt());
            
        }
    }
    public function createOldStuff($cat, $id, $exp_time){
        if($this->user != NULL && $this->user->getId() != 0){
            $q = Doctrine_Query::create()->
                    from("KTDOldStuff os")->
                    where("os.stuff_id = ?", $id)->
                    andWhere("os.category_name = ?", $cat)->
                    andwhere("os.player_id = ?", $this->user->getGuardUser()->getId())
                    ->count();
            if($q > 0)
                return NULL;
            $old = new KTDOldStuff();
            $old->setCategoryName($cat);
            $old->setSfGuardUser($this->user->getGuardUser());
            $old->setStuffId($id);
            $exp_time = strtotime($exp_time) + OldNewStuffManager::$_delay;
            
            $old->setExpirationTime(date("Y-m-d H:i:s", $exp_time));
            $old->save();
            return $old;
        }
        return NULL;
    }
    public function isUpdatedFiche(KTDCharacterFiche $fiche){
       if($this->user == NULL)
            return false;
       if($fiche == NULL)
            return false;
       if($this->compareTime($fiche->getUpdatedAt()))
        return false;
       else{
           if($this->hasOldStuff('fiche', $fiche->getId()))
                   return false;
           else
               return true;
       }
    }
    public function setOldFiche(KTDCharacterFiche $fiche){
        $old = $this->createOldStuff('fiche', $fiche->getId(), $fiche->getUpdatedAt());
        if(!$old)
            return false;
        
    }
    public function setOldAdventure(KTDAdventure $adv){
        if($this->user != NULL){
            $old = $this->createOldStuff("aventure", $adv->getLastAction()->getId(), $adv->getLastAction()->getCreatedAt());
            
        }
    }
    public function setOldTopic(KTDTopic $topic){
        if($this->user != NULL){
            $old = $this->createOldStuff("topic", $topic->getLastMessage()->getId(), $topic->getLastMessage()->getCreatedAt());
            
        }
    }
    private function hasOldStuff($category, $id){
        if($this->user != NULL && $this->old_stuff != NULL){
            $q = Doctrine_Query::create()->
                    from("KTDOldStuff os")->
                    where("os.stuff_id = ?", $id)
                    ->andwhere("os.category_name = ?", $category)
                    ->andWhere("os.player_id = ?", $this->user->getGuardUser()->getId())
                    ->count();
           if($q > 0)
               return true;
           else
                return false;
        }
        else
            return false;
    }
    private function loadOldStuff(){
        if($this->user != null){
            $this->old_stuff = $this->user->getGuardUser()->getOldStuff();
        }
    }
    private function cleanRecords(){
        if($this->user != NULL && $this->old_stuff != NULL && $this->user->isAuthenticated()){
            foreach($this->old_stuff as $old){
               if($this->user->getGuardUser()->getLastLogin() > $old->getExpirationTime())
                $old->delete();
            }
        }
    }
    public static function getSingleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
    private static $instance;
}
?>
